## R Code for Study 2, Lucid Data ##

# Load Study2_LucidData.csv #
ExpData <- read.table(file.choose(), header = TRUE, sep = ",")

# Identify Exp Data
ExpData$TransExp <- 0
ExpData$TransExp[!is.na(ExpData$Q93)] <- 1
ExpData$TransExp[!is.na(ExpData$Q94)] <- 1
table(ExpData$TransExp)

ExpData <- ExpData[ExpData$TransExp == 1, ]

# Code attitude, agree/disagree
ExpData$TransAttitude <- ifelse(!is.na(ExpData$Q93), ExpData$Q93,
                                ExpData$Q94)
table(ExpData$TransAttitude)

ExpData$TransAgree <- ifelse(ExpData$TransAttitude < 3, 1, 0)
table(ExpData$TransAgree)

# Code treatment cond (1 = episodic)
ExpData$TransTreat <- ifelse(!is.na(ExpData$Q94), 1, 0)
table(ExpData$TransTreat)

# Extremity coded high numbers if extreme #
ExpData$TransExtremity <- abs(3 - ExpData$TransAttitude)
table(ExpData$TransExtremity)
ExpData$TransExtremity <- ExpData$TransExtremity/2
table(ExpData$TransExtremity)

# Importance coded high numbers if important
ExpData$TransImportance <- 5 - ExpData$Q83
table(ExpData$TransImportance)
ExpData$TransImportance <- ExpData$TransImportance/4
table(ExpData$TransImportance)

# Personal Salience coded high numbers if decision affects you 
ExpData$TransPersonalSalience <- 5 - ExpData$Q84
table(ExpData$TransPersonalSalience)
ExpData$TransPersonalSalience <- ExpData$TransPersonalSalience/4
table(ExpData$TransPersonalSalience)

# Moral Conviction 1 -- High numbers indicate moralized
ExpData$TransMoral1 <- 5 - ExpData$Q85
table(ExpData$TransMoral1)

# Moral Conviction 2 -- High numbers indicate moralized
ExpData$TransMoral2 <- 5 - ExpData$Q86
table(ExpData$TransMoral2)

# Full Moral, High numbers means high moral
ExpData$TransMoralFull <- ExpData$TransMoral1 + ExpData$TransMoral2
table(ExpData$TransMoralFull)

ExpData$TransMoralFull <- ExpData$TransMoralFull/8
table(ExpData$TransMoralFull)

cor(ExpData$TransMoralFull, ExpData$TransPersonalSalience, use = "pairwise.complete.obs")
cor(ExpData$TransMoralFull, ExpData$TransImportance, use = "pairwise.complete.obs")
cor(ExpData$TransMoralFull, ExpData$TransExtremity, use = "pairwise.complete.obs")

ExpData$Anger <- ExpData$Q87_1
ExpData$Disgust <- ExpData$Q87_2
ExpData$Sadness <- ExpData$Q87_3
ExpData$Anxiety <- ExpData$Q87_4
ExpData$Fear <- ExpData$Q87_5

emodat <- c("Anger", "Disgust",
            "Sadness", "Anxiety", "Fear")

emodata <- ExpData[emodat]

emodata <- na.omit(emodata)

# Cronbach's alpha
library(psych)
alpha(emodata)

ExpData$AllEmo <- ExpData$Anger + ExpData$Disgust + 
  ExpData$Sadness + ExpData$Anxiety + ExpData$Fear
table(ExpData$AllEmo)
ExpData$AllEmo <- ExpData$AllEmo - 5
ExpData$AllEmo <- ExpData$AllEmo/20
table(ExpData$AllEmo)

# Code SUR Models
library(systemfit)

e1 <- TransMoralFull ~ TransTreat
e2 <- TransPersonalSalience ~ TransTreat
e3 <- TransImportance ~ TransTreat
e4 <- TransExtremity ~ TransTreat

SURVars <- c("TransMoralFull", "TransImportance", "TransPersonalSalience",
             "TransExtremity", "TransTreat")

ExpData1 <- ExpData[SURVars]
ExpData1 <- na.omit(ExpData1)

fitsur <- systemfit(list(Moral = e1, PerSal = e2, Imp = e3, Ext = e4), 
                    data=ExpData1)
summary(fitsur)

library(car)
rest1 <- "Moral_TransTreat - PerSal_TransTreat"
linearHypothesis(fitsur, rest1, test = "F")

rest2 <- "Moral_TransTreat - Imp_TransTreat"
linearHypothesis(fitsur, rest2, test = "F")

rest3 <- "Moral_TransTreat - Ext_TransTreat"
linearHypothesis(fitsur, rest3, test = "F")

# treatment effects by agree/disagree (supp material) #

e5 <- TransMoralFull ~ TransTreat*TransAgree
e6 <- TransPersonalSalience ~ TransTreat*TransAgree
e7 <- TransImportance ~ TransTreat*TransAgree
e8 <- TransExtremity ~ TransTreat*TransAgree

SURVars2 <- c("TransMoralFull", "TransImportance", "TransPersonalSalience",
              "TransExtremity", "TransTreat", "TransAgree")

ExpData2 <- ExpData[SURVars2]
ExpData3 <- na.omit(ExpData2)

fitsur2 <- systemfit(list(Moral = e5, PerSal = e6, Imp = e7, Ext = e8), 
                     data=ExpData3)
summary(fitsur2)

# Coding controls for med. analyses

ExpData$Ind <- 0
ExpData$Ind[ExpData$political_party == 4] <- 1
ExpData$Ind[ExpData$political_party == 7] <- 1

ExpData$Dem <- 0
ExpData$Dem[ExpData$political_party == 1] <- 1
ExpData$Dem[ExpData$political_party == 2] <- 1
ExpData$Dem[ExpData$political_party == 3] <- 1
ExpData$Dem[ExpData$political_party == 6] <- 1

ExpData$Rep <- 0
ExpData$Rep[ExpData$political_party == 5] <- 1
ExpData$Rep[ExpData$political_party == 8] <- 1
ExpData$Rep[ExpData$political_party == 9] <- 1
ExpData$Rep[ExpData$political_party == 10] <- 1

ExpData$Conserv <- ExpData$Q32
table(ExpData$Ind)
table(ExpData$Dem)
table(ExpData$Rep)
table(ExpData$Conserv)

ExpData$Female <- ifelse(ExpData$gender == 2, 1, 0)
table(ExpData$Female)

ExpData$Age <- ExpData$Q78
table(ExpData$Age)

MedVars <- c("TransTreat", "TransMoralFull", "AllEmo","TransExtremity",
             "TransImportance", "TransPersonalSalience", "Ind", "Dem",
             "Rep", "Conserv", "Female", "Age")

MedData <- ExpData[MedVars]
MedData <- na.omit(MedData)
MedData$Ideol <- MedData$Conserv

library(mediation)

set.seed(12345)
med.fit.emo <- lm(AllEmo ~ TransTreat + Dem + Rep + Ideol + Female + Age, 
                  data = MedData)
out.fit.emo <- lm(TransMoralFull ~ AllEmo + TransTreat + Dem + Rep + 
                    Ideol + Age + TransExtremity + TransImportance +
                    TransPersonalSalience, data = MedData)
med.out.emo <- mediate(med.fit.emo, out.fit.emo, treat = "TransTreat",
                       mediator = "AllEmo", robustSE = TRUE, 
                       sims = 2000)
summary(med.out.emo)

set.seed(12345)
med.fit.emo <- lm(AllEmo ~ TransTreat + Dem + Rep + Ideol + Female + Age, 
                  data = MedData)
out.fit.emo <- lm(TransImportance ~ AllEmo + TransTreat + Dem + Rep + 
                    Ideol + Age + TransExtremity + TransMoralFull +
                    TransPersonalSalience, data = MedData)
med.out.emo <- mediate(med.fit.emo, out.fit.emo, treat = "TransTreat",
                       mediator = "AllEmo", robustSE = TRUE, 
                       sims = 2000)
summary(med.out.emo)

set.seed(12345)
med.fit.emo <- lm(AllEmo ~ TransTreat + Dem + Rep + Ideol + Female + Age, 
                  data = MedData)
out.fit.emo <- lm(TransExtremity ~ AllEmo + TransTreat + Dem + Rep + 
                    Ideol + Age + TransMoralFull + TransImportance +
                    TransPersonalSalience, data = MedData)
med.out.emo <- mediate(med.fit.emo, out.fit.emo, treat = "TransTreat",
                       mediator = "AllEmo", robustSE = TRUE, 
                       sims = 2000)
summary(med.out.emo)

set.seed(12345)
med.fit.emo <- lm(AllEmo ~ TransTreat + Dem + Rep + Ideol + Female + Age, 
                  data = MedData)
out.fit.emo <- lm(TransPersonalSalience ~ AllEmo + TransTreat + Dem + Rep + 
                    Ideol + Age + TransExtremity + TransImportance +
                    TransMoralFull, data = MedData)
med.out.emo <- mediate(med.fit.emo, out.fit.emo, treat = "TransTreat",
                       mediator = "AllEmo", robustSE = TRUE, 
                       sims = 2000)
summary(med.out.emo)

# Multiple Mediators (supp material) #

### Moral Conviction ###

# Anger #
set.seed(12345)
m.med_anger_moralconv95 <- multimed(outcome = "TransMoralFull", med.main = "Anger",
                                    med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                                    treat = "TransTreat",
                                    covariates = c("TransExtremity", "TransImportance",
                                                   "TransPersonalSalience",
                                                   "Dem", "Rep", "Ideol", 
                                                   "Female", "Age"),
                                    data = MedData, sims = 2000,
                                    conf.level = .95)
summary(m.med_anger_moralconv95)

set.seed(12345)
m.med_anger_moralconv99 <- multimed(outcome = "TransMoralFull", med.main = "Anger",
                                    med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                                    treat = "TransTreat",
                                    covariates = c("TransExtremity", "TransImportance",
                                                   "TransPersonalSalience",
                                                   "Dem", "Rep", "Ideol", 
                                                   "Female", "Age"),
                                    data = MedData, sims = 2000,
                                    conf.level = .99)
summary(m.med_anger_moralconv99)

# Disgust #
set.seed(12345)
m.med_disgust_moralconv95 <- multimed(outcome = "TransMoralFull", med.main = "Disgust",
                                      med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .95)
summary(m.med_disgust_moralconv95)

set.seed(12345)
m.med_disgust_moralconv99 <- multimed(outcome = "TransMoralFull", med.main = "Disgust",
                                      med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .99)
summary(m.med_disgust_moralconv99)


# Anxiety #
set.seed(12345)
m.med_anxiety_moralconv95 <- multimed(outcome = "TransMoralFull", med.main = "Anxiety",
                                      med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .95)
summary(m.med_anxiety_moralconv95)

set.seed(12345)
m.med_anxiety_moralconv99 <- multimed(outcome = "TransMoralFull", med.main = "Anxiety",
                                      med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .99)
summary(m.med_anxiety_moralconv99)

# Sadness #
set.seed(12345)
m.med_sadness_moralconv95 <- multimed(outcome = "TransMoralFull", med.main = "Sadness",
                                      med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .95)
summary(m.med_sadness_moralconv95)

set.seed(12345)
m.med_sadness_moralconv99 <- multimed(outcome = "TransMoralFull", med.main = "Sadness",
                                      med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                      treat = "TransTreat",
                                      covariates = c("TransExtremity", "TransImportance",
                                                     "TransPersonalSalience",
                                                     "Dem", "Rep", "Ideol", 
                                                     "Female", "Age"),
                                      data = MedData, sims = 2000,
                                      conf.level = .99)
summary(m.med_sadness_moralconv99)


# Fear #
set.seed(12345)
m.med_fear_moralconv95 <- multimed(outcome = "TransMoralFull", med.main = "Fear",
                                   med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransPersonalSalience",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .95)
summary(m.med_fear_moralconv95)

set.seed(12345)
m.med_fear_moralconv99 <- multimed(outcome = "TransMoralFull", med.main = "Fear",
                                   med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransPersonalSalience",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .99)
summary(m.med_fear_moralconv99)


### Personal Salience ###

# Anger #
set.seed(12345)
m.med_anger_persal95 <- multimed(outcome = "TransPersonalSalience", med.main = "Anger",
                                 med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                                 treat = "TransTreat",
                                 covariates = c("TransExtremity", "TransImportance",
                                                "TransMoralFull",
                                                "Dem", "Rep", "Ideol", 
                                                "Female", "Age"),
                                 data = MedData, sims = 2000,
                                 conf.level = .95)
summary(m.med_anger_persal95)

set.seed(12345)
m.med_anger_persal99 <- multimed(outcome = "TransPersonalSalience", med.main = "Anger",
                                 med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                                 treat = "TransTreat",
                                 covariates = c("TransExtremity", "TransImportance",
                                                "TransMoralFull",
                                                "Dem", "Rep", "Ideol", 
                                                "Female", "Age"),
                                 data = MedData, sims = 2000,
                                 conf.level = .99)
summary(m.med_anger_persal99)

# Disgust #
set.seed(12345)
m.med_disgust_persal95 <- multimed(outcome = "TransPersonalSalience", med.main = "Disgust",
                                   med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .95)
summary(m.med_disgust_persal95)

set.seed(12345)
m.med_disgust_persal99 <- multimed(outcome = "TransPersonalSalience", med.main = "Disgust",
                                   med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .99)
summary(m.med_disgust_persal99)

# Anxiety #
set.seed(12345)
m.med_anxiety_persal95 <- multimed(outcome = "TransPersonalSalience", med.main = "Anxiety",
                                   med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .95)
summary(m.med_anxiety_persal95)

set.seed(12345)
m.med_anxiety_persal99 <- multimed(outcome = "TransPersonalSalience", med.main = "Anxiety",
                                   med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .99)
summary(m.med_anxiety_persal99)

# Sadness #
set.seed(12345)
m.med_sadness_persal95 <- multimed(outcome = "TransPersonalSalience", med.main = "Sadness",
                                   med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .95)
summary(m.med_sadness_persal95)

set.seed(12345)
m.med_sadness_persal99 <- multimed(outcome = "TransPersonalSalience", med.main = "Sadness",
                                   med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                   treat = "TransTreat",
                                   covariates = c("TransExtremity", "TransImportance",
                                                  "TransMoralFull",
                                                  "Dem", "Rep", "Ideol", 
                                                  "Female", "Age"),
                                   data = MedData, sims = 2000,
                                   conf.level = .99)
summary(m.med_sadness_persal99)


# Fear #
set.seed(12345)
m.med_fear_persal95 <- multimed(outcome = "TransPersonalSalience", med.main = "Fear",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransImportance",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_fear_persal95)

set.seed(12345)
m.med_fear_persal99 <- multimed(outcome = "TransPersonalSalience", med.main = "Fear",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransImportance",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_fear_persal99)


### Attitude Importance ###

# Anger #
set.seed(12345)
m.med_anger_imp95 <- multimed(outcome = "TransImportance", med.main = "Anger",
                              med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                              treat = "TransTreat",
                              covariates = c("TransExtremity", "TransPersonalSalience",
                                             "TransMoralFull",
                                             "Dem", "Rep", "Ideol", 
                                             "Female", "Age"),
                              data = MedData, sims = 2000,
                              conf.level = .95)
summary(m.med_anger_imp95)

set.seed(12345)
m.med_anger_imp99 <- multimed(outcome = "TransImportance", med.main = "Anger",
                              med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                              treat = "TransTreat",
                              covariates = c("TransExtremity", "TransPersonalSalience",
                                             "TransMoralFull",
                                             "Dem", "Rep", "Ideol", 
                                             "Female", "Age"),
                              data = MedData, sims = 2000,
                              conf.level = .99)
summary(m.med_anger_imp99)

# Disgust #
set.seed(12345)
m.med_disgust_imp95 <- multimed(outcome = "TransImportance", med.main = "Disgust",
                                med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_disgust_imp95)

set.seed(12345)
m.med_disgust_imp99 <- multimed(outcome = "TransImportance", med.main = "Disgust",
                                med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_disgust_imp99)

# Anxiety #
set.seed(12345)
m.med_anxiety_imp95 <- multimed(outcome = "TransImportance", med.main = "Anxiety",
                                med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_anxiety_imp95)

set.seed(12345)
m.med_anxiety_imp99 <- multimed(outcome = "TransImportance", med.main = "Anxiety",
                                med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_anxiety_imp99)

# Sadness #
set.seed(12345)
m.med_sadness_imp95 <- multimed(outcome = "TransImportance", med.main = "Sadness",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_sadness_imp95)

set.seed(12345)
m.med_sadness_imp99 <- multimed(outcome = "TransImportance", med.main = "Sadness",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransExtremity", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_sadness_imp99)

# Fear #
set.seed(12345)
m.med_fear_imp95 <- multimed(outcome = "TransImportance", med.main = "Fear",
                             med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                             treat = "TransTreat",
                             covariates = c("TransExtremity", "TransPersonalSalience",
                                            "TransMoralFull",
                                            "Dem", "Rep", "Ideol", 
                                            "Female", "Age"),
                             data = MedData, sims = 2000,
                             conf.level = .95)
summary(m.med_fear_imp95)

set.seed(12345)
m.med_fear_imp99 <- multimed(outcome = "TransImportance", med.main = "Fear",
                             med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                             treat = "TransTreat",
                             covariates = c("TransExtremity", "TransPersonalSalience",
                                            "TransMoralFull",
                                            "Dem", "Rep", "Ideol", 
                                            "Female", "Age"),
                             data = MedData, sims = 2000,
                             conf.level = .99)
summary(m.med_fear_imp99)


# Attitude Extremity #

# Anger #
set.seed(12345)
m.med_anger_ext95 <- multimed(outcome = "TransExtremity", med.main = "Anger",
                              med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                              treat = "TransTreat",
                              covariates = c("TransImportance", "TransPersonalSalience",
                                             "TransMoralFull",
                                             "Dem", "Rep", "Ideol", 
                                             "Female", "Age"),
                              data = MedData, sims = 2000,
                              conf.level = .95)
summary(m.med_anger_ext95)

set.seed(12345)
m.med_anger_ext99 <- multimed(outcome = "TransExtremity", med.main = "Anger",
                              med.alt = c("Disgust", "Sadness", "Anxiety", "Fear"),
                              treat = "TransTreat",
                              covariates = c("TransImportance", "TransPersonalSalience",
                                             "TransMoralFull",
                                             "Dem", "Rep", "Ideol", 
                                             "Female", "Age"),
                              data = MedData, sims = 2000,
                              conf.level = .99)
summary(m.med_anger_ext99)

# Disgust #
set.seed(12345)
m.med_disgust_ext95 <- multimed(outcome = "TransExtremity", med.main = "Disgust",
                                med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_disgust_ext95)

set.seed(12345)
m.med_disgust_ext99 <- multimed(outcome = "TransExtremity", med.main = "Disgust",
                                med.alt = c("Anger", "Sadness", "Anxiety", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_disgust_ext99)

# Anxiety #
set.seed(12345)
m.med_anxiety_ext95 <- multimed(outcome = "TransExtremity", med.main = "Anxiety",
                                med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_anxiety_ext95)

set.seed(12345)
m.med_anxiety_ext99 <- multimed(outcome = "TransExtremity", med.main = "Anxiety",
                                med.alt = c("Anger", "Sadness", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_anxiety_ext99)

# Sadness #
set.seed(12345)
m.med_sadness_ext95 <- multimed(outcome = "TransExtremity", med.main = "Sadness",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .95)
summary(m.med_sadness_ext95)

set.seed(12345)
m.med_sadness_ext99 <- multimed(outcome = "TransExtremity", med.main = "Sadness",
                                med.alt = c("Anger", "Anxiety", "Disgust", "Fear"),
                                treat = "TransTreat",
                                covariates = c("TransImportance", "TransPersonalSalience",
                                               "TransMoralFull",
                                               "Dem", "Rep", "Ideol", 
                                               "Female", "Age"),
                                data = MedData, sims = 2000,
                                conf.level = .99)
summary(m.med_sadness_ext99)


# Fear #
set.seed(12345)
m.med_fear_ext95 <- multimed(outcome = "TransExtremity", med.main = "Fear",
                             med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                             treat = "TransTreat",
                             covariates = c("TransImportance", "TransPersonalSalience",
                                            "TransMoralFull",
                                            "Dem", "Rep", "Ideol", 
                                            "Female", "Age"),
                             data = MedData, sims = 2000,
                             conf.level = .95)
summary(m.med_fear_ext95)

set.seed(12345)
m.med_fear_ext99 <- multimed(outcome = "TransExtremity", med.main = "Fear",
                             med.alt = c("Anger", "Anxiety", "Disgust", "Sadness"),
                             treat = "TransTreat",
                             covariates = c("TransImportance", "TransPersonalSalience",
                                            "TransMoralFull",
                                            "Dem", "Rep", "Ideol", 
                                            "Female", "Age"),
                             data = MedData, sims = 2000,
                             conf.level = .99)
summary(m.med_fear_ext99)

###

# Clearing data, reloading

ExpData <- read.table(file.choose(), header = TRUE, sep = ",")

ExpData$TransExp <- 0
ExpData$TransExp[!is.na(ExpData$Q93)] <- 1
ExpData$TransExp[!is.na(ExpData$Q94)] <- 1
table(ExpData$TransExp)

ExpData <- ExpData[ExpData$TransExp == 1, ]

# value at 10th percentile is 228
quantile(ExpData$Duration, c(.1))
ExpData$Short <- ifelse(ExpData$Duration < 228, 1, 0)
table(ExpData$Short)

# coding straightlining, q62-66
table(ExpData$Q62) # coded 1-5

table(ExpData$Q63) #coded 12468
ExpData$Q63_1[ExpData$Q63 == 1] <- 1
ExpData$Q63_1[ExpData$Q63 == 2] <- 2
ExpData$Q63_1[ExpData$Q63 == 4] <- 3
ExpData$Q63_1[ExpData$Q63 == 6] <- 4
ExpData$Q63_1[ExpData$Q63 == 8] <- 5
table(ExpData$Q63_1)

table(ExpData$Q64) # coded 1-5

table(ExpData$Q65)
ExpData$Q65_1[ExpData$Q65 == 8] <- 1
ExpData$Q65_1[ExpData$Q65 == 9] <- 2
ExpData$Q65_1[ExpData$Q65 == 10] <- 3
ExpData$Q65_1[ExpData$Q65 == 11] <- 4
ExpData$Q65_1[ExpData$Q65 == 12] <- 5
table(ExpData$Q65_1)

table(ExpData$Q66)
ExpData$Q66_1[ExpData$Q66 == 8] <- 1
ExpData$Q66_1[ExpData$Q66 == 9] <- 2
ExpData$Q66_1[ExpData$Q66 == 10] <- 3
ExpData$Q66_1[ExpData$Q66 == 11] <- 4
ExpData$Q66_1[ExpData$Q66 == 12] <- 5
table(ExpData$Q66_1)

ExpData$Straight <- ifelse(ExpData$Q62 == ExpData$Q63_1 &
                             ExpData$Q62 == ExpData$Q64 &
                             ExpData$Q62 == ExpData$Q65_1 &
                             ExpData$Q62 == ExpData$Q66_1, 1, 0)
table(ExpData$Straight)


ExpData$TransAttitude <- ifelse(!is.na(ExpData$Q93), ExpData$Q93,
                                ExpData$Q94)
table(ExpData$TransAttitude)

ExpData$TransAgree <- ifelse(ExpData$TransAttitude < 3, 1, 0)
table(ExpData$TransAgree)

ExpData$TransTreat <- ifelse(!is.na(ExpData$Q94), 1, 0)
table(ExpData$TransTreat)

# Extremity coded high numbers if extreme #
ExpData$TransExtremity <- abs(3 - ExpData$TransAttitude)
table(ExpData$TransExtremity)
ExpData$TransExtremity <- ExpData$TransExtremity/2
table(ExpData$TransExtremity)

# Importance coded high numbers if important
ExpData$TransImportance <- 5 - ExpData$Q83
table(ExpData$TransImportance)
ExpData$TransImportance <- ExpData$TransImportance/4
table(ExpData$TransImportance)

# Personal Salience coded high numbers if decision affects you 
ExpData$TransPersonalSalience <- 5 - ExpData$Q84
table(ExpData$TransPersonalSalience)
ExpData$TransPersonalSalience <- ExpData$TransPersonalSalience/4
table(ExpData$TransPersonalSalience)

# Moral Conviction 1 -- High numbers indicate moralized
ExpData$TransMoral1 <- 5 - ExpData$Q85
table(ExpData$TransMoral1)

# Moral Conviction 2 -- High numbers indicate moralized
ExpData$TransMoral2 <- 5 - ExpData$Q86
table(ExpData$TransMoral2)

# Full Moral, High numbers means high moral
ExpData$TransMoralFull <- ExpData$TransMoral1 + ExpData$TransMoral2
table(ExpData$TransMoralFull)

ExpData$TransMoralFull <- ExpData$TransMoralFull/8
table(ExpData$TransMoralFull)

cor(ExpData$TransMoralFull, ExpData$TransPersonalSalience, use = "pairwise.complete.obs")
cor(ExpData$TransMoralFull, ExpData$TransImportance, use = "pairwise.complete.obs")
cor(ExpData$TransMoralFull, ExpData$TransExtremity, use = "pairwise.complete.obs")

ExpData$Anger <- ExpData$Q87_1
ExpData$Disgust <- ExpData$Q87_2
ExpData$Sadness <- ExpData$Q87_3
ExpData$Anxiety <- ExpData$Q87_4
ExpData$Fear <- ExpData$Q87_5

emodat <- c("Anger", "Disgust",
            "Sadness", "Anxiety", "Fear")

emodata <- ExpData[emodat]

emodata <- na.omit(emodata)

library(psych)
alpha(emodata)

ExpData$AllEmo <- ExpData$Anger + ExpData$Disgust + 
  ExpData$Sadness + ExpData$Anxiety + ExpData$Fear
table(ExpData$AllEmo)
ExpData$AllEmo <- ExpData$AllEmo - 5
ExpData$AllEmo <- ExpData$AllEmo/20
table(ExpData$AllEmo)

library(systemfit)

e9 <- TransMoralFull ~ TransTreat*Short
e10 <- TransPersonalSalience ~ TransTreat*Short
e11 <- TransImportance ~ TransTreat*Short
e12 <- TransExtremity ~ TransTreat*Short

SURVars3 <- c("TransMoralFull", "TransImportance", "TransPersonalSalience",
              "TransExtremity", "TransTreat", "Short")

ExpData4 <- ExpData[SURVars3]
ExpData5 <- na.omit(ExpData4)

fitsur3 <- systemfit(list(Moral = e9, PerSal = e10, Imp = e11, Ext = e12), 
                     data=ExpData5)
summary(fitsur3)

## Subsetting by Straightlining ##

e13 <- TransMoralFull ~ TransTreat*Straight
e14 <- TransPersonalSalience ~ TransTreat*Straight
e15 <- TransImportance ~ TransTreat*Straight
e16 <- TransExtremity ~ TransTreat*Straight

SURVars4 <- c("TransMoralFull", "TransImportance", "TransPersonalSalience",
              "TransExtremity", "TransTreat", "Straight")

ExpData6 <- ExpData[SURVars4]
ExpData7 <- na.omit(ExpData6)

fitsur4 <- systemfit(list(Moral = e13, PerSal = e14, Imp = e15, Ext = e16), 
                     data=ExpData7)
summary(fitsur4)
